﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace YJYpublicClass
{
    
    class LogInfo
    {
        public string Time { get; set; }
        public string Info { get; set; }
    }
   
    public class DataLogging
    {
        private  RichTextBox _coloredTextBox;
        string Name = "Log";
        string LogFilePath = "";
        public int MaxLines = 1000;
        object objLock = new object();

        public DataLogging(string LogFile,string myName)
        {
            Name = myName;
            LogFilePath = LogFile+ "Logs\\"; 
        }

        public void AssignListView(RichTextBox logLV)
        {
            _coloredTextBox = logLV;
        }
        public void AddLog(string Info)
        {
            lock (objLock)
            {
                SaveLog(Info);

                if (_coloredTextBox != null)
                {
                    WriteLine(Info);
                }
            }
        }
        public void AddLog(string Info,Color color)
        {
            lock (objLock)
            {
                SaveLog(Info);

                if (_coloredTextBox != null)
                {
                    WriteLine(color,Info);
                }
            }
        }
            /// <summary>
            /// 正常颜色显示
            /// </summary>
            /// <param name="formatString"></param>
            /// <param name="args"></param>
        public void WriteLine(string formatString, params object[] args)
        {
            var data = string.Format(formatString, args);



            if (_coloredTextBox.InvokeRequired)
            {
                Action ac = new Action(() =>
                    {
                        AutoClear();

                        // _coloredTextBox.AppendText(data + Environment.NewLine);
                        _coloredTextBox.SelectionColor = Color.Black;
                        _coloredTextBox.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff   ") + data + "\r\n");
                        if (_coloredTextBox.Lines.Length > MaxLines)
                        {
                            var rm = new List<int>();
                            for (int i = 0; i < MaxLines / 2; i++)
                            {
                                rm.Add(i);
                            }

                        }
                    });
                _coloredTextBox.Invoke(ac);
            }
            else
            {
                AutoClear();

                // _coloredTextBox.AppendText(data + Environment.NewLine);
                _coloredTextBox.SelectionColor = Color.Black;
                _coloredTextBox.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff   ") + data + "\r\n");
                if (_coloredTextBox.Lines.Length > MaxLines)
                {
                    var rm = new List<int>();
                    for (int i = 0; i < MaxLines / 2; i++)
                    {
                        rm.Add(i);
                    }

                }
            }
        }

            public  void AutoClear()
            {
                int line = _coloredTextBox.TextLength;
                if (line >= 1024 * 10)
                {
                    _coloredTextBox.Clear();
                    line = 0;
                }
            }

            /// <summary>
            /// 可变颜色显示
            /// </summary>
            /// <param name="color">red, yellow, white, green</param>
            /// <param name="formatString"></param>
            /// <param name="args"></param>
            public void WriteLine(Color color, string formatString, params object[] args)
            {
                var data = string.Format(formatString, args);


                if (_coloredTextBox.InvokeRequired)
                {
                    Action ac = new Action(() =>
                {
                    AutoClear();
                    _coloredTextBox.SelectionColor = color;

                    _coloredTextBox.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff   ") + data + "\r\n");

                });
                    _coloredTextBox.Invoke(ac);
                }
            }
            private void SaveLog(string msg)
        {
           
            string logFileName = "";
            
           
            FileStream objFileStream = null;
            StreamWriter sw = null;

            try
            {
                //string LogFilePath = MchConfig.DataSavingPath + "Logs\\";
                if (!Directory.Exists(LogFilePath))
                {
                    Directory.CreateDirectory(LogFilePath);
                }
                string Filename = "";
                Filename = LogFilePath + DateTime.Now.ToString("yyyyMMdd") + "\\";
                if (!Directory.Exists(Filename))
                {
                    Directory.CreateDirectory(Filename);
                }
                logFileName = LogFilePath + Name + "_" + DateTime.Now.ToString("yyyyMMdd") + ".csv";

                if (!File.Exists(logFileName))
                {
                    objFileStream = new FileStream(logFileName, FileMode.CreateNew, FileAccess.Write);
                }
                else
                {
                    objFileStream = new FileStream(logFileName, FileMode.Append, FileAccess.Write);
                }

                sw = new StreamWriter(objFileStream, System.Text.Encoding.GetEncoding(-0));


                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " , " + msg);
                sw.Close();
                objFileStream.Close();
            }
            catch (Exception)
            {
                sw.Close();
                objFileStream.Close();
            }

        }
    }

}
